<?php

class DB {
	
	var $error = null;
	
	function DB($config = array()) {
		extract($config);
		$this->db = @mysql_connect($host, $user, $password);
		if(!$this->db) $this->error = 'Could not connect to server';
		if (!@mysql_select_db($database, $this->db)) $this->error = 'Could not select database';

	}
	
	function close() {
        $this->db = !@mysql_close($this->db);
        return !$this->db;
    }

	function query($sql='') {
		$this->id_query = @mysql_query($sql, $this->db);
		return $this->id_query;
	}
	
	function fetch_row($object = true) {
		if($object) return @mysql_fetch_object($this->id_query);
		return @mysql_fetch_array($this->id_query);
	}
	
	function fetch_rows($assoc = true) {
		$rows = array();
		while($row = $this->fetch_row($assoc)) $rows[] = $row;
		return $rows;
	}
	
	function last_affected() {
		if($this->id_query) {
			return mysql_affected_rows($this->db);
		}
		return null;
	}
	
	function last_id() {
		if($this->id_query) {
			return mysql_insert_id($this->db);
		}
		return null;
	}
	
	function last_num_rows() {
        if ($this->id_query) {
            return @mysql_num_rows($this->id_query);
        }
        return null;
    }

	function fetch_seek($row=0) {
		if($this->id_query) {
			return @mysql_data_seek($this->id_query, $row);
		}
		return null;
	}
	
	function get_field($field, $row=0) {
		return @mysql_result($this->id_query,$row,$field);
	}
	
	function get_fields($table='') {
		$this->query('SHOW COLUMNS FROM '.$table);
		return $this->fetch_rows();
	}
	
	function name_field($number) {
		return mysql_field_name($this->id_query, $number);
	}
	
	function escape($str='') {
		if (get_magic_quotes_gpc()) {
			$str = stripslashes($str); 
		}
		return mysql_real_escape_string($str);
	}
}

?>